import 'package:flutter/material.dart';

const cityNames = [
  '北京','南京','杭州','上海','深圳','广州','合肥','太原','济南','南昌','乌鲁木齐','沈阳','哈尔滨','长春'
];

class GridviewPage extends StatefulWidget {
  const GridviewPage({super.key});

  @override
  State<GridviewPage> createState() => _GridviewPageState();
}

class _GridviewPageState extends State<GridviewPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('演示GridView列表')),
      body: GridView.count(
          crossAxisCount: 3,// 横向显示的元素数量
          children: _buildList(),
      ),
    );
  }

  List<Widget> _buildList() {
    return cityNames.map((city) => _item(city)).toList();
  }

  Widget _item(String city) {
    return Container(
      height: 80,
      margin: const EdgeInsets.only(bottom: 5,right: 5,left:5),
      alignment: Alignment.center,
      decoration: const BoxDecoration(color: Colors.amber),
      child: Text(city,style: const TextStyle(color: Colors.white,fontSize: 20),),
    );
  }
}
